package com.CTAtracker.android.Fragments;

import java.util.ArrayList;

import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.CTAtracker.android.R;
import com.CTAtracker.android.Activities.StationActivity;
import com.CTAtracker.android.Adapters.StopItemAdapter;
import com.CTAtracker.android.DAO.StationDataSource;
import com.CTAtracker.android.Models.ColorStation;
import com.CTAtracker.android.Models.Station;
import com.CTAtracker.android.Models.StopETA;

/**
 * The fragment of station link to the Station activity
 */
public class StationFragment extends Fragment {
	/**
	 * The instance handling the connection to the database
	 */
	private StationDataSource _stationDataSource;
	/**
	 * The list of stop on the station and their Estimate Time of Arrival
	 */
	private ArrayList<StopETA> _listStop;
	/**
	 * The view of the list of stop
	 */
	private ListView _listViewStation;
	/**
	 * The station we want to display the ETA
	 */
	private Station _station;
	/**
	 * The color of the line of the station
	 */
	private ColorStation _color; 
	/**
	 * The station activity
	 */
	private StationActivity _stationActivity;
	/**
	 * The adapter of the list view 
	 */
	private StopItemAdapter _adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		_stationDataSource = new StationDataSource(container.getContext());
		_station = ((StationActivity) getActivity()).getStation();
		_stationActivity = (StationActivity) getActivity();
		_color = new ColorStation(_station.get_line_id()-1);
		_listStop= new ArrayList<StopETA>();
		_adapter = null;
		return inflater.inflate(R.layout.look_up_station, container, false);
	}

	@Override
	public void onStart() {
		super.onStart();

		// deal with the layout
		// Linear Layout
		LinearLayout linearLayout = (LinearLayout) _stationActivity.findViewById(R.id.titleMainStation);
		linearLayout.setBackgroundColor(Color.parseColor(_color.toString()));
		// TextView
		TextView stationName = (TextView) _stationActivity.findViewById(R.id.textViewStationActivity);
		stationName.setText(_station.get_name());


		_stationDataSource.open();

		// get the stop id of the station
		ArrayList<Integer> listStopId = _stationDataSource.getListStopId(
				_station.get_parent_stop_id(), _station.get_line_id());
		for (int i =0; i<listStopId.size(); i++){
			_listStop.add(new StopETA(
					_stationDataSource.getStationByStopID(listStopId.get(i), _station.get_line_id()), getActivity()));
		}
		_stationDataSource.close();

		_listViewStation = (ListView) getView().findViewById(R.id.listStationMain);
		_adapter = new StopItemAdapter(
				_stationActivity, R.layout.fragment_station, _listStop);

		_listViewStation.setAdapter(_adapter);

	}
}
